RelativeJobView = Ext.extend(Ext.Panel, {
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initUIComponents();
		RelativeJobView.superclass.constructor.call(this, {
			id : "RelativeJobView",
			title : "上下级管理",
			region : "center",
			layout : "border",
			iconCls : "menu-relativeJob",
			items : [ this.treePanel, this.searchPanel, this.gridPanel ]
		});
	},
	initUIComponents : function() {
		this.searchPanel = new Ext.FormPanel({
			id : "relativeJobSearchPanel",
			height : 35,
			frame : false,
			border : false,
			region : "north",
			layout : "hbox",
			layoutConfig : {
				padding : "5px",
				align : "middle"
			},
			defaults : {
				xtype : "label",
				border : false,
				margins : {
					top : 0,
					left : 4,
					right : 4,
					bottom : 4
				}
			},
			keys : {
				key : Ext.EventObject.ENTER,
				fn : this.search.createCallback(this),
				scope : this
			},
			items : [ {
				text : "用户账号："
			}, {
				xtype : "textfield",
				name : "Q_username_S_LK",
				maxLength : 256
			}, {
				text : "用户名称："
			}, {
				xtype : "textfield",
				name : "Q_fullname_S_LK",
				maxLength : 256
			}, {
				text : "入职时间:从"
			}, {
				xtype : "datefield",
				format : "Y-m-d",
				name : "Q_accessionTime_D_GT"
			}, {
				text : "至"
			}, {
				xtype : "datefield",
				format : "Y-m-d",
				name : "Q_accessionTime_D_LT"
			}, {
				xtype : "button",
				text : "搜索",
				scope : this,
				iconCls : "search",
				handler : this.search.createCallback(this)
			}, {
				xtype : "button",
				text : "清空",
				scope : this,
				iconCls : "reset",
				handler : this.reset
			} ]
		});
		this.treePanel = new Ext.tree.TreePanel({
			id : "relativeJobViewTreePanel",
			region : "west",
			collapsible : true,
			autoScroll : true,
			split : true,
			width : 150,
			title : "部门信息列表",
			tbar : new Ext.Toolbar({
				items : [ {
					xtype : "button",
					iconCls : "btn-refresh",
					text : "刷新",
					handler : function() {
						Ext.getCmp("relativeJobViewTreePanel").root.reload();
					}
				}, {
					xtype : "button",
					text : "展开",
					iconCls : "btn-expand",
					handler : function() {
						Ext.getCmp("relativeJobViewTreePanel").expandAll();
					}
				}, {
					xtype : "button",
					text : "收起",
					iconCls : "btn-collapse",
					handler : function() {
						Ext.getCmp("relativeJobViewTreePanel").collapseAll();
					}
				} ]
			}),
			loader : new Ext.tree.TreeLoader({
				url : __ctxPath + "/system/listDepartment.do"
			}),
			root : new Ext.tree.AsyncTreeNode({
				expanded : true
			}),
			rootVisible : false,
			listeners : {
				"click" : function(g) {
					if (g != null) {
						var f = Ext.getCmp("RelativeJobView").gridPanel
								.getStore();
						f.url = __ctxPath + "/system/listAppUser.do";
						f.baseParams = {
							"Q_department.depId_L_EQ" : g.id
						};
						f.reload({
							params : {
								start : 0,
								limit : 25
							}
						});
					}
				}
			}
		});
		if (isGranted("_RelativeJobAdd") || isGranted("_RelativeJobEdit")
				|| isGranted("_RelativeJobDel")) {
			this.treePanel.on("contextmenu", b, this.treePanel);
		}
		var e = new Ext.menu.Menu({
			id : "RelativeJobTreeMenu",
			items : [ {
				text : "新建部门",
				iconCls : "btn-add",
				scope : this,
				handler : c
			}, {
				text : "修改部门信息",
				iconCls : "btn-edit",
				scope : this,
				handler : a
			}, {
				text : "删除部门",
				iconCls : "btn-delete",
				scope : this,
				handler : d
			} ]
		});
		function b(f, g) {
			selected = new Ext.tree.TreeNode({
				id : f.id,
				text : f.text
			});
			e.showAt(g.getXY());
		}
		function c() {
			var f = selected.id;
			var g = Ext.getCmp("departmentForm");
			if (g == null) {
				if (f > 0) {
					new DepartmentForm({
						nodeId : f
					}).show();
				} else {
					new DepartmentForm({
						nodeId : 0
					}).show();
				}
			}
		}
		function d() {
			var f = selected.id;
			var g = Ext.getCmp("relativeJobViewTreePanel");
			if (f > 0) {
				Ext.Msg.confirm("删除操作", "你确定删除部门?", function(h) {
					if (h == "yes") {
						Ext.Ajax.request({
							url : __ctxPath
									+ "/system/removeDepartment.do?depId=" + f,
							success : function(i, k) {
								var j = Ext.util.JSON.decode(i.responseText);
								if (j.success) {
									Ext.ux.Toast.msg("操作提示", "删除成功!");
									g.root.reload();
								} else {
									Ext.ux.Toast.msg("操作提示", "对不起，删除");
								}
							},
							failure : function(i, j) {
							}
						});
					}
				});
			} else {
				Ext.ux.Toast.msg("警告", "总公司不能被删除");
			}
		}
		function a() {
			var f = selected.id;
			if (f > 0) {
				var g = Ext.getCmp("departmentForm");
				if (g == null) {
					new DepartmentForm().show();
					g = Ext.getCmp("departmentForm");
				}
				g.form.load({
					url : __ctxPath + "/system/detailDepartment.do",
					params : {
						depId : f
					},
					method : "post",
					deferredRender : true,
					layoutOnTabChange : true,
					success : function() {
					},
					failure : function() {
						Ext.ux.Toast.msg("编辑", "载入失败");
					}
				});
			} else {
				Ext.ux.Toast.msg("警告", "总公司不能修改！");
			}
		}
		this.topbar = new Ext.Toolbar({
			defaultType : "button",
			items : [ {
				iconCls : "btn-del",
				text : "删除用户信息",
				scope : this,
				handler : this.removeSelRs
			} ]
		});
		this.gridPanel = new HT.GridPanel({
			title : "用户信息列表",
			region : "center",
			tbar : this.topbar,
			rowActions : true,
			id : "RelativeJobGrid",
			url : __ctxPath + "/system/listAppUser.do?",
			fields : [ {
				name : "userId",
				type : "int"
			}, "username", "address", "fullname", "email", "department",
					"position", "accessionTime" ],
			columns : [ {
				header : "userId",
				dataIndex : "userId",
				hidden : true
			}, {
				header : "账号",
				dataIndex : "username",
				width : 60
			}, {
				header : "地址",
				dataIndex : "address",
				hidden : true,
				exprint : true
			}, {
				header : "用户名",
				dataIndex : "fullname",
				width : 60
			}, {
				header : "邮箱",
				dataIndex : "email",
				width : 120
			}, {
				header : "所属部门",
				dataIndex : "department",
				renderer : function(f) {
					return f == null ? "" : f.depName;
				},
				width : 60
			}, {
				header : "入职时间",
				dataIndex : "accessionTime",
				width : 100
			}, new Ext.ux.grid.RowActions({
				header : "管理",
				width : 100,
				actions : [ {
					iconCls : "btn-relativeJob",
					qtip : "添加上下级",
					style : "margin:0 3px 0 3px"
				}, {
					iconCls : "btn-del",
					qtip : "删除",
					style : "margin:0 3px 0 3px"
				} ],
				listeners : {
					scope : this,
					"action" : this.onRowAction
				}
			}) ]
		});
//		this.gridPanel.addListener("rowdblclick", this.rowClick);
	},
	rowClick : function(b, a, c) {
		b.getSelectionModel().each(function(d) {
			new RelativeUserView({
				userId : d.data.userId,
				username : d.data.username,
				depId : d.data.department.depId
			}).show();
		});
	},
	createRs : function() {
		new RelativeJobForm().show();
	},
	removeRs : function(a) {
		$postDel({
			url : __ctxPath + "/system/multiDelAppUser.do",
			ids : a,
			grid : this.gridPanel
		});
	},
	removeSelRs : function() {
		$delGridRs({
			url : __ctxPath + "/system/multiDelAppUser.do",
			grid : this.gridPanel,
			idName : "userId"
		});
	},
	addRelativeJob : function(a) {
		new RelativeUserView({
			userId : a.data.userId,
			username : a.data.username,
			depId : a.data.department.depId
		}).show();
	},
	onRowAction : function(c, a, d, e, b) {
		switch (d) {
		case "btn-relativeJob":
			this.addRelativeJob.call(this, a);
			break;
		case "btn-del":
			this.removeRs.call(this, a.data.userId);
			break;
		default:
			break;
		}
	},
	reset : function() {
		var a = Ext.getCmp("relativeJobSearchPanel");
		a.getForm().reset();
	},
	search : function(c) {
		var a = Ext.getCmp("relativeJobSearchPanel");
		var b = Ext.getCmp("RelativeJobGrid");
		if (a.getForm().isValid()) {
			$search({
				searchPanel : a,
				gridPanel : b
			});
		}
	}
});